package department.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oracle.jrockit.jfr.RequestDelegate;

import department.domain.Department;
import department.service.DepartmentService;

public class RegisterDepartmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
//		1. 요청파라미터 조회 (부서_id, 부서명, 지점)
		String departmentId = request.getParameter("departmentId");
		String departmentName = request.getParameter("departmentName");
		String location = request.getParameter("location");
		
		
//		2. 업무로직 처리 - DepartmentDAO 메소드 이용
		DepartmentService service = DepartmentService.getInstance();	
		Department department = new Department(departmentId, departmentName, location);
		
		try {
			service.registerDepartment(department);
			//새로고침시 다시 등록 처리 되는 것을 막기위해 Redirect 방식으로 이동시킴
			response.sendRedirect("/DepartmentManager/GetDepartmentServlet?departmentId="+departmentId);
		} catch (SQLException e) {
			e.printStackTrace();
			request.setAttribute("error_message", e.getMessage());
			RequestDispatcher rdp = request.getRequestDispatcher("/error.jsp");
			rdp.forward(request, response);
		}
	
	}

}



















